package com.nineclock.auth.intergration.processor.impl;

import cn.hutool.core.util.StrUtil;
import com.nineclock.auth.intergration.entity.IntergrationAuthenticationEntity;
import com.nineclock.auth.intergration.processor.AuthenticationProcessor;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.system.dto.SysUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 账号密码认证处理器
 */
@Component
public class UsernameAndPasswordAuthenticationProcessor implements AuthenticationProcessor {

    @Autowired
    private SysUserFeign sysUserFeign;

    @Override
    public Boolean support(IntergrationAuthenticationEntity entity) {
        if(entity != null && StrUtil.isEmpty(entity.getAuthType())){
            return true;
        }
        return false;
    }


    @Override
    public SysUserDTO authenticate(IntergrationAuthenticationEntity entity) {
        //获取参数
        String username = entity.getAuthParameter("username");

        //判定
        if(StrUtil.isEmpty(username)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //根据传递 手机号-用户名 获取用户信息
        SysUserDTO userDTO = sysUserFeign.query(username).getData();

        //组装并返回
        return userDTO;
    }

}
